//
//  UIWindow+HUD.m
//  SWS
//
//  Created by shi_mhua on 16/3/24.
//  Copyright © 2016年 zjszyms. All rights reserved.
//

#import "UIWindow+HUD.h"
#import "MBProgressHUD.h"
#import <objc/runtime.h>

const void *windowProgressHUDKey = &windowProgressHUDKey;

@interface UIView ()<MBProgressHUDDelegate>

@property (nonatomic, strong) MBProgressHUD *progressHUD;

@end

@implementation UIWindow (HUD)

// 重写set/get方法
-(void)setProgressHUD:(MBProgressHUD *)progressHUD
{
    objc_setAssociatedObject(self, &windowProgressHUDKey, progressHUD, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(MBProgressHUD *)progressHUD
{
    MBProgressHUD *hud = objc_getAssociatedObject(self, &windowProgressHUDKey);
    if (!hud)
    {
        hud = [[MBProgressHUD alloc] initWithWindow:self];
        hud.opacity = 0.8;
        hud.removeFromSuperViewOnHide = YES;
        hud.labelFont = [UIFont systemFontOfSize:14.0f];
        [self addSubview:hud];
        hud.delegate = self;
        self.progressHUD = hud;
    }
    return hud;
}

- (void)showProgress
{
    [self showProgress:@"加载中..."];
}

- (void)showProgress:(NSString *)message
{
    self.progressHUD.detailsLabelText = message;
    [self.progressHUD show:YES];
}

- (void)showProgress:(NSString *)message duration:(int)duration
{
    self.progressHUD.detailsLabelText = message;
    [self.progressHUD show:YES];
    [self.progressHUD hide:YES afterDelay:duration];
}

- (void)showToastMessage:(NSString *)message
{
    [self showToastMessage:message duration:3];
}

- (void)showToastMessage:(NSString *)message duration:(int)duration
{
    self.progressHUD.labelText = message;
    self.progressHUD.mode = MBProgressHUDModeText;
    [self.progressHUD show:YES];
    [self.progressHUD hide:YES afterDelay:duration];
}

- (void)showSuccessMessage
{
    [self showSuccessMessage:nil duration:3];
}

- (void)showSuccessMessage:(NSString *)message
{
    [self showSuccessMessage:message duration:3];
}

- (void)showSuccessMessage:(NSString *)message duration:(int)duration
{
    self.progressHUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"success.png"]];
    self.progressHUD.mode = MBProgressHUDModeCustomView;
    self.progressHUD.detailsLabelText = message;
    [self.progressHUD show:YES];
    [self.progressHUD hide:YES afterDelay:duration];
}

- (void)showErrorMessage
{
    [self showErrorMessage:nil duration:3];
}

- (void)showErrorMessage:(NSString *)message
{
    [self showErrorMessage:message duration:3];
}

- (void)showErrorMessage:(NSString *)message duration:(int)duration
{
    self.progressHUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"failed.png"]];
    self.progressHUD.mode = MBProgressHUDModeCustomView;
    self.progressHUD.detailsLabelText = message;
    [self.progressHUD show:YES];
    [self.progressHUD hide:YES afterDelay:duration];
}

- (void)hideHud
{
    [self.progressHUD hide:YES];
}

/**
 * Called after the HUD was fully hidden from the screen.
 */
- (void)hudWasHidden:(MBProgressHUD *)hud
{
    self.progressHUD = nil;
}

@end

